<%@ WebHandler Language="C#" Class="ImageHandler" %>

//==========================================================================
// New HTTPHandler to read image from database and display for premium users
// Author: Deepak
// Date: 18 Jan 2008
//==========================================================================

using System;
using System.Web;
using SiteManager;
using System.Data.SqlClient;

public class ImageHandler : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {
        Manager objMan = new SiteManager.Manager();

        int id = Convert.ToInt32(context.Request.QueryString["id"]);
        string type = context.Request.QueryString["type"];
        string sql = "Select Image_Content from tbl_drpremium where dr_id=" + id;
        if (string.Compare(type, "t", true) == 0)
            sql = "Select th_Image from tbl_drpremium where dr_id=" + id;

        SqlDataReader dr = objMan.ExecuteQuery(sql);
        //byte[] drA;
        byte[] defaultImg;
        if (dr.Read())
        {
            if (dr[0].ToString() != "")
            {
                context.Response.BinaryWrite((byte[])dr[0]);
            }
            else
            {
                 //Case: NO IMAGE AVAILABLE
                System.IO.FileStream fs; 
                if (context.Request.QueryString["gender"].ToUpper() == "M")
                    fs = new System.IO.FileStream(context.Server.MapPath("images/Dr_images/silhouette-male.gif"), System.IO.FileMode.Open, System.IO.FileAccess.Read);
                else
                    fs = new System.IO.FileStream(context.Server.MapPath("images/Dr_images/silhouette--female.gif"), System.IO.FileMode.Open, System.IO.FileAccess.Read);                   
                
                defaultImg = new Byte[fs.Length];
                fs.Read(defaultImg, 0, (int)fs.Length);
                fs.Flush();
                fs.Dispose();

                context.Response.BinaryWrite((byte[])defaultImg);  
            }
        }
        else
        {
            // Case: NOT A PREMIUM MEMBER
          //  context.Response.Write("adasdasda" + context.Request.QueryString["gender"]);
            
            System.IO.FileStream fs;
            if (context.Request.QueryString["gender"].ToUpper() == "M")
            {
                fs = new System.IO.FileStream(context.Server.MapPath("images/Dr_images/silhouette-male.gif"), System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //    context.Response.Write("in if" + context.Request.QueryString["gender"]);
            }
            else
            {
                fs = new System.IO.FileStream(context.Server.MapPath("images/Dr_images/silhouette--female.gif"), System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //    context.Response.Write("<br>in else<br>" + context.Request.QueryString["gender"]);
            }
            
            defaultImg = new Byte[fs.Length];
            fs.Read(defaultImg, 0, (int)fs.Length);
            fs.Flush();
            fs.Dispose();

            context.Response.BinaryWrite((byte[])defaultImg);  
        }
        dr.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}